package com.childmeter.controller;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.childmeter.entity.JsonChildmeter;
import com.childmeter.service.ChildmeterService;
import com.entity.ChildMeter;
import com.file.entity.FileMeter;
import com.utils.JsonResult;

/**
 * @Title: ChildmeterController.java
 * @Package com.childmeter.controller
 * @author 孙梦楠
 * @date 2017年10月16日 下午1:30:01
 * @version V1.0.0
 */
@RestController
@RequestMapping(value = "/father_child")
public class ChildmeterController {

	@Autowired
	private ChildmeterService childmeterservice;

	// 列出所有未设置的表
	@RequestMapping(value = "/list_meters", method = RequestMethod.GET)
	public JsonResult<List<FileMeter>> listmeters(
			@CookieValue(value = "opeorgcookie", required = false) String opeorgcookie,
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie,
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {

		return childmeterservice.listmeter2(opeorgcookie, opetypecookie, languagecookie);
	}

	// 列出符合org的所有总表
	@RequestMapping(value = "/list_fathermeter", method = RequestMethod.GET)
	public JsonResult<List<FileMeter>> listfathermeter(
			@CookieValue(value = "opeorgcookie", required = false) String opeorgcookie,
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie,
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {

		return childmeterservice.listfathermeter(opeorgcookie, opetypecookie, languagecookie);
	}

	// 列出符合org的所有分表
	@RequestMapping(value = "/list_childmeter", method = RequestMethod.GET)
	public JsonResult<List<FileMeter>> listchildmeter(
			@CookieValue(value = "opeorgcookie", required = false) String opeorgcookie,
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie,
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {

		return childmeterservice.listchildmeter(opeorgcookie, opetypecookie, languagecookie);
	}

	// 设置总表
	@RequestMapping(value = "/setfather", method = RequestMethod.POST)
	public JsonResult<ChildMeter> setFather(@RequestBody JsonChildmeter meters) {

		List<ChildMeter> fatherchildmeters = meters.getMeters();
		String type = "1";// 总表

		return childmeterservice.setFatherChildmeter(fatherchildmeters, type);
	}

	// 设置分表
	@RequestMapping(value = "/setchild", method = RequestMethod.POST)
	public JsonResult<ChildMeter> setChild(@RequestBody JsonChildmeter meters) {

		List<ChildMeter> fatherchildmeters = meters.getMeters();
		String type = "2";// 分表

		return childmeterservice.setFatherChildmeter(fatherchildmeters, type);
	}

	// 关联总分表
	@RequestMapping(value = "/connect", method = RequestMethod.POST)
	public JsonResult<ChildMeter> connect(@RequestBody JsonChildmeter JsonChildmeter) {

		Integer fathermeterid = JsonChildmeter.getFathermeterid();
		List<ChildMeter> meters = JsonChildmeter.getMeters();

		return childmeterservice.connect(fathermeterid, meters);
	}

	// 清除分表关联
	@RequestMapping(value = "/delect", method = RequestMethod.POST)
	public JsonResult<ChildMeter> delect1(@RequestBody JsonChildmeter meter) {
		Integer meterid = meter.getMeterid();

		return childmeterservice.delect(meterid);
	}

	// 小区下的总分表结构
	@RequestMapping(value = "/list_fathermeter_by_area", method = RequestMethod.GET)
	public JsonResult<List<JsonChildmeter>> listconnectedfathermeter(
			@CookieValue(value = "opeorgcookie", required = false) String opeorgcookie,
			@CookieValue(value = "opetypecookie", required = false) String opetypecookie,
			@CookieValue(value = "languagecookie", required = false) String languagecookie) {

		return childmeterservice.listFatherAndChildMeterByArea(opeorgcookie, opetypecookie, languagecookie);
	}

	// 根据总表列出下面的分表
	@RequestMapping(value = "/list_childmeter_by_fathermeter", method = RequestMethod.POST)
	public JsonResult<List<JsonChildmeter>> listChildMeterByFatherMeter(@RequestBody JsonChildmeter JsonChildmeter) {

		Integer fathermeterid = JsonChildmeter.getMeterid();
		return childmeterservice.listChildMeterByFatherMeter(fathermeterid);
	}

}
